"""Prepend sys.path with file's directory if not already included"""
self.runcommand('if 1:\n _filename = %r\n import sys as _sys\n from os.path import dirname as _dirname\n _dir = _dirname(_filename)\n if not _dir in _sys.path:\n _sys.path.insert(0, _dir)\n del _filename, _sys, _dirname, _dir\n \n' % (filename,))
def showsyntaxerror(self, filename = None):
'''Extend base class method: Add Colorizing
Color the offending position instead of printing it and pointing at it
if tkMessageBox.askyesno('Exit?', 'Do you want to exit altogether?', default = 'yes', master = self.tkconsole.text):
raise
else:
self.showtraceback()
except:
tkMessageBox.askyesno('Exit?', 'Do you want to exit altogether?', default = 'yes', master = self.tkconsole.text)
self.showtraceback()
finally:
if not use_subprocess:
self.tkconsole.endexecuting()
def write(self, s):
'''Override base class method'''
self.tkconsole.stderr.write(s)
def display_port_binding_error(self):
tkMessageBox.showerror('Port Binding Error', "IDLE can't bind TCP/IP port 8833, which is necessary to communicate with its Python execution server. Either no networking is installed on this computer or another process (another IDLE?) is using the port. Run IDLE with the -n command line switch to start without a subprocess and refer to Help/IDLE Help 'Running without a subprocess' for further details.", master = self.tkconsole.text)
def display_no_subprocess_error(self):
tkMessageBox.showerror('Subprocess Startup Error', "IDLE's subprocess didn't make connection. Either IDLE can't start a subprocess or personal firewall software is blocking the connection.", master = self.tkconsole.text)
def display_executing_dialog(self):
tkMessageBox.showerror('Already executing', 'The Python Shell window is already executing a command; please wait until it is finished.', master = self.tkconsole.text)
class PyShell(OutputWindow):
shell_title = 'Python Shell'
ColorDelegator = ModifiedColorDelegator
UndoDelegator = ModifiedUndoDelegator
menu_specs = [
('file', '_File'),
('edit', '_Edit'),
('debug', '_Debug'),
('options', '_Options'),
('windows', '_Windows'),
('help', '_Help')]
from IdleHistory import History
def __init__(self, flist = None):
if use_subprocess:
ms = self.menu_specs
if ms[2][0] != 'shell':
ms.insert(2, ('shell', '_Shell'))
self.interp = ModifiedInterpreter(self)
if flist is None:
root = Tk()
fixwordbreaks(root)
root.withdraw()
flist = PyShellFileList(root)
OutputWindow.__init__(self, flist, None, None)
import __builtin__
__builtin__.quit = __builtin__.exit = 'To exit, type Ctrl-D.'
'''Extend EditorWindow._close(), shut down debugger and execution server'''
self.close_debugger()
if use_subprocess:
self.interp.kill_subprocess()
sys.stdout = self.save_stdout
sys.stderr = self.save_stderr
sys.stdin = self.save_stdin
self.interp = None
self.console = None
self.flist.pyshell = None
self.history = None
EditorWindow._close(self)
def ispythonsource(self, filename):
'''Override EditorWindow method: never remove the colorizer'''
return True
def short_title(self):
return self.shell_title
COPYRIGHT = 'Type "copyright", "credits" or "license()" for more information.'
firewallmessage = "\n ****************************************************************\n Personal firewall software may warn about the connection IDLE\n makes to its subprocess using this computer's internal loopback\n interface. This connection is not visible on any external\n interface and no data is sent to or received from the Internet.\n ****************************************************************\n "
if self.text.compare('sel.first', '!=', 'sel.last'):
return None
except:
pass
if not self.executing or self.reading:
self.resetoutput()
self.interp.write('KeyboardInterrupt\n')
self.showprompt()
return 'break'
self.endoffile = 0
self.canceled = 1
if self.executing and self.interp.rpcclt:
if self.interp.getdebugger():
self.interp.restart_subprocess()
else:
self.interp.interrupt_subprocess()
if self.reading:
self.top.quit()
return 'break'
def eof_callback(self, event):
if self.executing and not (self.reading):
return None
if not self.text.compare('iomark', '==', 'insert') and self.text.compare('insert', '==', 'end-1c'):
return None
if not self.executing:
self.resetoutput()
self.close()
else:
self.canceled = 0
self.endoffile = 1
self.top.quit()
return 'break'
def home_callback(self, event):
if event.state != 0 and event.keysym == 'Home':
return None
if self.text.compare('iomark', '<=', 'insert') and self.text.compare('insert linestart', '<=', 'iomark'):
self.text.mark_set('insert', 'iomark')
self.text.tag_remove('sel', '1.0', 'end')
self.text.see('insert')
return 'break'
def linefeed_callback(self, event):
if self.reading:
self.text.insert('insert', '\n')
self.text.see('insert')
else:
self.newline_and_indent_event(event)
return 'break'
def enter_callback(self, event):
if self.executing and not (self.reading):
return None
try:
sel = self.text.get('sel.first', 'sel.last')
if sel:
if self.text.compare('sel.last', '<=', 'iomark'):
self.recall(sel)
return 'break'
except:
pass
if self.text.compare('insert', '<', 'iomark linestart'):
prev = self.text.tag_prevrange('stdin', 'insert')
if prev and self.text.compare('insert', '<', prev[1]):
self.recall(self.text.get(prev[0], prev[1]))
return 'break'
next = self.text.tag_nextrange('stdin', 'insert')
if next and self.text.compare('insert lineend', '>=', next[0]):
self.recall(self.text.get(next[0], next[1]))
return 'break'
line = self.text.get('insert linestart', 'insert lineend')
last_line_of_prompt = sys.ps1.split('\n')[-1]
if line.startswith(last_line_of_prompt):
line = line[len(last_line_of_prompt):]
self.recall(line)
return 'break'
if self.text.compare('insert', '<', 'iomark'):
self.text.mark_set('insert', 'iomark')
s = self.text.get('insert', 'end-1c')
if s and not s.strip():
self.text.delete('insert', 'end-1c')
if self.text.compare('insert', '<', 'end-1c linestart'):
self.newline_and_indent_event(event)
return 'break'
self.text.mark_set('insert', 'end-1c')
if self.reading:
self.text.insert('insert', '\n')
self.text.see('insert')
else:
self.newline_and_indent_event(event)
self.text.tag_add('stdin', 'iomark', 'end-1c')
self.text.update_idletasks()
if self.reading:
self.top.quit()
else:
self.runit()
return 'break'
def recall(self, s):
if self.history:
self.history.recall(s)
def runit(self):
line = self.text.get('iomark', 'end-1c')
i = len(line)
while i > 0 and line[i - 1] in ' \t':
i = i - 1
if i > 0 and line[i - 1] == '\n':
i = i - 1
while i > 0 and line[i - 1] in ' \t':
i = i - 1
line = line[:i]
more = self.interp.runsource(line)
def open_stack_viewer(self, event = None):
if self.interp.rpcclt:
return self.interp.remote_stack_viewer()
try:
sys.last_traceback
except:
tkMessageBox.showerror('No stack trace', 'There is no stack trace yet.\n(sys.last_traceback is not defined)', master = self.text)
return None
StackBrowser = StackBrowser
import StackViewer
sv = StackBrowser(self.root, self.flist)
def view_restart_mark(self, event = None):
self.text.see('iomark')
self.text.see('restart')
def restart_shell(self, event = None):
self.interp.restart_subprocess()
def showprompt(self):
self.resetoutput()
try:
s = str(sys.ps1)
except:
s = ''
self.console.write(s)
self.text.mark_set('insert', 'end-1c')
self.set_line_and_column()
self.io.reset_undo()
def resetoutput(self):
source = self.text.get('iomark', 'end-1c')
if self.history:
self.history.history_store(source)
if self.text.get('end-2c') != '\n':
self.text.insert('end-1c', '\n')
self.text.mark_set('iomark', 'end-1c')
self.set_line_and_column()
sys.stdout.softspace = 0
def write(self, s, tags = ()):
try:
self.text.mark_gravity('iomark', 'right')
OutputWindow.write(self, s, tags, 'iomark')
self.text.mark_gravity('iomark', 'left')
except:
pass
if self.canceled:
self.canceled = 0
if not use_subprocess:
raise KeyboardInterrupt
class PseudoFile:
def __init__(self, shell, tags, encoding = None):
self.shell = shell
self.tags = tags
self.softspace = 0
self.encoding = encoding
def write(self, s):
self.shell.write(s, self.tags)
def writelines(self, l):
map(self.write, l)
def flush(self):
pass
def isatty(self):
return True
usage_msg = '\nUSAGE: idle [-deins] [-t title] [file]*\n idle [-dns] [-t title] (-c cmd | -r file) [arg]*\n idle [-dns] [-t title] - [arg]*\n\n -h print this help message and exit\n -n run IDLE without a subprocess (see Help/IDLE Help for details)\n\nThe following options will override the IDLE \'settings\' configuration:\n\n -e open an edit window\n -i open a shell window\n\nThe following options imply -i and will open a shell:\n\n -c cmd run the command in a shell, or\n -r file run script from file\n\n -d enable the debugger\n -s run $IDLESTARTUP or $PYTHONSTARTUP before anything else\n -t title set title of shell window\n\nA default edit window will be bypassed when -c, -r, or - are used.\n\n[arg]* are passed to the command (-c) or script (-r) in sys.argv[1:].\n\nExamples:\n\nidle\n Open an edit window or shell depending on IDLE\'s configuration.\n\nidle foo.py foobar.py\n Edit the files, also open a shell if configured to start with shell.\n\nidle -est "Baz" foo.py\n Run $IDLESTARTUP or $PYTHONSTARTUP, edit foo.py, and open a shell\n window with the title "Baz".\n\nidle -c "import sys; print sys.argv" "foo"\n Open a shell window and run the command, passing "-c" in sys.argv[0]\n and "foo" in sys.argv[1].\n\nidle -d -s -r foo.py "Hello World"\n Open a shell window, run a startup script, enable the debugger, and\n run foo.py, passing "foo.py" in sys.argv[0] and "Hello World" in\n sys.argv[1].\n\necho "import sys; print sys.argv" | idle - "foobar"\n Open a shell window, run the script piped in, passing \'\' in sys.argv[0]\n and "foobar" in sys.argv[1].\n'
def main():
global use_subprocess, use_subprocess, root, flist